package com.jctrip.hotel.source.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.domain.dao.HtOrderSourceRecordDao;
import com.jctrip.hotel.domain.entity.HtOrderSourceRecord;
import com.jctrip.hotel.source.model.Result;
import com.jctrip.hotel.source.model.hmein.req.CheckAvailableReq;
import com.jctrip.hotel.source.model.hmein.req.CreateOrderReq;
import com.jctrip.hotel.source.model.hmein.req.QueryOrderStatusReq;
import com.jctrip.hotel.source.model.hmein.resp.*;
import com.jctrip.hotel.source.service.hmein.HmeinService;
import com.jctrip.hotel.source.service.hmein.HmeinTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

@Slf4j
@Api(tags = "hmein")
@RestController
@RequestMapping("/hmein")
@ApiSupport(order = 6)
public class HmeinController {
    @Resource
    private HmeinService hmeinService;
    @Autowired
    private HmeinTaskService hmeinTaskService;

    @GetMapping("/getCityData")
    @ApiOperation(value = "getCityData", notes = "获取城市编码")
    @ApiOperationSupport(order = 1)
    public Result getCityData(@RequestParam(value = "appId", required = false) String appId) {
        List<CityListResult.CityList> result;
        try {
            result = hmeinService.getCityData(appId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/getHotelIds")
    @ApiOperation(value = "getHotelIds", notes = "获取酒店列表")
    @ApiOperationSupport(order = 2)
    public Result getHotelIds(@RequestParam(value = "appId", required = false) String appId) {
        List<String> result;
        try {
            result = hmeinService.getHotelIds(appId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/getHotelInfo")
    @ApiOperation(value = "getHotelInfo", notes = "获取酒店信息")
    @ApiOperationSupport(order = 3)
    public Result getHotelInfo(@RequestParam(value = "appId", required = false) String appId,
                               @RequestParam(value = "hotelId", required = false) String hotelId) {
        HotelInfosResult.HotelInfos result;
        try {
            result = hmeinService.getHotelInfo(appId, hotelId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/getRoomType")
    @ApiOperation(value = "getRoomType", notes = "获取酒店房型")
    @ApiOperationSupport(order = 4)
    public Result getRoomType(@RequestParam(value = "appId", required = false) String appId,
                              @RequestParam(value = "hotelId", required = false) String hotelId) {
        List<HotelRmMcResult.HotelRmMc> result;
        try {
            result = hmeinService.getRoomType(appId, hotelId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/getHotelPic")
    @ApiOperation(value = "查酒店图片", notes = "获取酒店房型")
    @ApiOperationSupport(order = 5)
    public Result getHotelPic(@RequestParam(value = "appId", required = false) String appId,
                              @RequestParam(value = "hotelId", required = false) String hotelId) {
        List<String> result;
        try {
            result = hmeinService.getHotelPic(appId, hotelId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/getHotelFacilitiesListData")
    @ApiOperation(value = "getHotelFacilitiesListData", notes = "获取酒店")
    @ApiOperationSupport(order = 6)
    public Result getHotelFacilitiesListData(@RequestParam(value = "appId", required = false) String appId,
                                             @RequestParam(value = "hotelId", required = false) String hotelId) {
        List<HotelFacilitiesResult.HotelFacilitiesList> result;
        try {
            result = hmeinService.getHotelFacilitiesListData(appId, hotelId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/getRoomStatus")
    @ApiOperation(value = "getRoomStatus", notes = "获取酒店房态")
    @ApiOperationSupport(order = 7)
    public Result getRoomStatus(@RequestParam(value = "appId", required = false) String appId,
                                @RequestParam(value = "memberNo", required = false) String memberNo,
                                @RequestParam(value = "hotelId", required = false) String hotelId,
                                @RequestParam(value = "roomTypeId", required = false) String roomTypeId,
                                @RequestParam(value = "start", required = false) String start,
                                @RequestParam(value = "end", required = false) String end) {
        RoomStatusResult result;
        try {
            result = hmeinService.getRoomStatus(appId, memberNo, hotelId, roomTypeId, start, end);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/checkAvailability")
    @ApiOperation(value = "checkAvailability", notes = "试单")
    @ApiOperationSupport(order = 8)
    public Result checkAvailability(@RequestBody CheckAvailableReq req) {
        CheckAvailableResult result;
        try {
            result = hmeinService.checkAvailability(req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/createOrder")
    @ApiOperation(value = "createOrder", notes = "创建订单")
    @ApiOperationSupport(order = 9)
    public Result createOrder(@RequestBody CreateOrderReq req) {
        CreateOrderResult result;
        try {
            result = hmeinService.createOrder(req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

//    @GetMapping("/postOrder")
//    @ApiOperation(value = "postOrder", notes = "下单")
//    @ApiOperationSupport(order = 9)
//    OrderDataResult postOrder(CreateOrderReq req, PriceCheckParam priceCheck){
//        return hmeinService.postOrder(req, priceCheck);
//    }

    @PostMapping("/queryOrderStatus")
    @ApiOperation(value = "queryOrderStatus", notes = "查询订单状态")
    @ApiOperationSupport(order = 10)
    public Result queryOrderStatus(@RequestParam(value = "appId", required = false) String appId,
                                   @RequestBody QueryOrderStatusReq req) {
        QueryOrderStatusResult result;
        try {
            result = hmeinService.queryOrderStatus(appId, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/cancelOrder")
    @ApiOperation(value = "cancelOrder", notes = "取消订单")
    @ApiOperationSupport(order = 11)
    public Result cancelOrder(@RequestParam(value = "appId", required = false) String appId,
                              @RequestParam(value = "orderNo", required = false) String orderNo) {
        Boolean result;
        try {
            result = hmeinService.cancelOrder(appId, orderNo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    //================接口直连================
    @GetMapping("/queryDirectOrderStatus")
    @ApiOperation(value = "queryDirectOrderStatus", notes = "接口直连查询订单状态")
    public Result queryDirectOrderStatus(@RequestParam(value = "sourceId") @ApiParam("供应商账户id") String sourceId,
                                         @RequestParam(value = "outOrderNo") @ApiParam("订单编号") String outOrderNo) {
        QueryOrderStatusResult queryOrderStatusResult;
        try {
            QueryOrderStatusReq req = new QueryOrderStatusReq();
            req.setOutOrderNo(outOrderNo.trim());
            HtOrderSourceRecordDao htOrderSourceRecordDao =
                    BeanContext.getApplicationContext().getBean(HtOrderSourceRecordDao.class);
            HtOrderSourceRecord sourceRecord = Optional.ofNullable(htOrderSourceRecordDao.selectOne(new LambdaQueryWrapper<HtOrderSourceRecord>()
                    .eq(HtOrderSourceRecord::getBookId, outOrderNo.trim()))).orElse(new HtOrderSourceRecord());
            req.setOrderNo(sourceRecord.getSourceOrderId());
            queryOrderStatusResult = hmeinService.queryDirectOrderStatus(sourceId, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(queryOrderStatusResult);
    }

    @GetMapping("/savePic")
    @ApiOperation(value = "保存图片")
    public Result<String> savePic(@RequestParam String appId,
                                  @RequestParam String hotelId) throws IOException {
        hmeinTaskService.syncHmeinPic(appId, hotelId);
        return Result.ok("success");
    }

    @GetMapping("/getBrands")
    @ApiOperation(value = "获取酒店品牌")
    public Result<List<GetHmeinBrandListResult.BrandList>> getBrands(@RequestParam String appId) {
        return Result.ok(hmeinService.getBrand(appId));
    }
}
